package com.tfleo.mealssenger;

import java.util.ArrayList;
import java.util.Calendar;

import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.service.ReceiveNotificationService;
import com.tfleo.mealssenger.service.UpdateLocationService;

import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends ListActivity {
	private static final long KEEP_ALIVE_INTERVAL = 1000 * 60 * 60; //HOURLY
	public static final int RECEIVE_NOTIF = 0;
	public static final int AVAILABLE_CHAT = 1;
	public static final int UPDATE_LOCATION = 2;
	ArrayList<String> prefItems = new ArrayList<String>();
	SharedPreferences appSettings;
	SharedPreferences.Editor prefEditor;  
	
	private PendingIntent pendingIntent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preferences_layout);
		appSettings = getSharedPreferences(Main.APP_DATA, MODE_PRIVATE);
		prefEditor = appSettings.edit();
		prefItems.add("Receive notification");
		prefItems.add("Update location");
		
		PrefAdapter adapter = new PrefAdapter(this, prefItems);
		
		setListAdapter(adapter);
	}
	
	public class PrefAdapter extends ArrayAdapter<String> {
		private LayoutInflater inflater;
		
		public PrefAdapter(Context context, ArrayList<String> items) {
			super(context, 0, items);
			inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			v = inflater.inflate(R.layout.preferences_list_entry, null);
			
			final TextView itemView = (TextView) v.findViewById(R.id.pref_item);
			final CheckBox checkBoxView = (CheckBox) v.findViewById(R.id.pref_check);
			
			itemView.setText(prefItems.get(position));
			
			switch(position) {
			case RECEIVE_NOTIF:
				if(appSettings.getBoolean(Main.RECEIVE_NOTIF, false)) {
					checkBoxView.setChecked(true);
				} else {
					checkBoxView.setChecked(false);
				}
				setCheckBoxReceiveNotif(checkBoxView);
				break;
			case UPDATE_LOCATION:
				if(appSettings.getBoolean(Main.UPDATE_LOCATION, false)) {
					checkBoxView.setChecked(true);
				} else {
					checkBoxView.setChecked(false);
				}
				setCheckBoxUpdateLocation(checkBoxView);
				break;
			}
			
			return v;	
		}


		private void setCheckBoxReceiveNotif(CheckBox checkBoxView) {
			checkBoxView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					//TODO
			        if (((CheckBox) v).isChecked()) {
			            Toast.makeText(getApplicationContext(), "Receive notif", Toast.LENGTH_SHORT).show();
			            prefEditor.putBoolean(Main.RECEIVE_NOTIF, true);
			            prefEditor.commit();
			            ReceiveNotificationService.actionStart(getApplicationContext());
			        } else {
			            Toast.makeText(getApplicationContext(), "Not receive notif", Toast.LENGTH_SHORT).show();
			            prefEditor.putBoolean(Main.RECEIVE_NOTIF, false);
			            prefEditor.commit();
			            ReceiveNotificationService.actionStop(getApplicationContext());
			        }
				}
			});
			
		}
		
		private void setCheckBoxUpdateLocation(CheckBox checkBoxView) {
			checkBoxView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					//TODO
			        if (((CheckBox) v).isChecked()) {
			            Toast.makeText(getApplicationContext(), "Update location", Toast.LENGTH_SHORT).show();
			            prefEditor.putBoolean(Main.UPDATE_LOCATION, true);
			            prefEditor.commit();
			            processAlarm();
			            //ReceiveNotificationService.actionStart(getApplicationContext());
			        } else {
			            Toast.makeText(getApplicationContext(), "No update location", Toast.LENGTH_SHORT).show();
			            prefEditor.putBoolean(Main.UPDATE_LOCATION, false);
			            prefEditor.commit();
			            killAlarm();
			            //ReceiveNotificationService.actionStop(getApplicationContext());
			        } 
				}
			});
			
		}
		
	}
	
	protected void processAlarm() {
		 Intent myIntent = new Intent(this, UpdateLocationService.class);
		 pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
		 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

		 //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
		 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + KEEP_ALIVE_INTERVAL,
					KEEP_ALIVE_INTERVAL , pendingIntent);
		 //Toast.makeText(SettingsActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
	}
	
	protected void killAlarm() {
		AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
		alarmManager.cancel(pendingIntent);
		this.stopService(new Intent(this, UpdateLocationService.class));

		// Tell the user about what we did.
		//Toast.makeText(SettingsActivity.this, "Cancel!", Toast.LENGTH_LONG).show();
	}
}
